package com.perkinelmer.housecall.client.activities.patientdetails;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.perkinelmer.housecall.client.activities.capture.Capture;
import com.perkinelmer.housecall.client.activities.capture.Capture.CaptureId;
import com.perkinelmer.housecall.client.activities.capture.CaptureActivity;
import com.perkinelmer.housecall.client.activities.capture.CaptureLibrary;
import com.perkinelmer.housecall.client.activities.selectpatient.SelectPatientActivity;
import com.perkinelmer.housecall.client.activity.Activity;
import com.perkinelmer.housecall.client.activity.ActivityManager;
import com.perkinelmer.housecall.client.model.Observation;
import com.perkinelmer.housecall.client.model.Patient;

public class PatientDetailsActivity implements Activity<Patient> {

	public static final ActivityId<Patient> ID = ActivityId.make("PatientDetails");

	private IncidentTable _incidentTable = new IncidentTable();
	private Patient _patient;
	private VerticalPanel _mainVerticalPanel = new VerticalPanel();
	private CaptureLibrary _captureLibrary;
	private ActivityManager _manager;
	
	public PatientDetailsActivity(CaptureLibrary captureLibrary, ActivityManager manager)
	{
		_captureLibrary=captureLibrary;
		_manager = manager;
	}
	
	@Override
	public ActivityId<Patient> getId() {
		return ID;
	}

	public Patient getPatient() {return _patient;}
	
	private Panel createCapturesPanel(HorizontalPanel buttonPanel) {
		VerticalPanel panel = new VerticalPanel();
		buttonPanel.setSpacing(5);
		
		Label title = new Label("Captures");
		title.getElement().addClassName("details_title");
		
		panel.add(title);
		panel.add(buttonPanel);
		
		return panel;
	}
	
	private Panel createIncidentTablePanel() {
		VerticalPanel panel = new VerticalPanel();
		Label title = new Label("Observations");
		title.getElement().addClassName("details_title");
		
		panel.add(title);
		panel.add(_incidentTable);
		
		return panel;
	}
	
	@Override
	public void start(Panel panel, Patient patient) {
		
		if (_patient != null && _patient.equals(patient))
		{
			panel.add(_mainVerticalPanel);
			return;
		} else {_patient = patient;}

		_mainVerticalPanel = new VerticalPanel();
		_mainVerticalPanel.setSpacing(10);
	    
		final HorizontalPanel backButtonpanel = new HorizontalPanel();
	    final HorizontalPanel panel1 = new HorizontalPanel();
	    final HorizontalPanel submitPanel = new HorizontalPanel();
	    final HorizontalPanel buttonPanel = new HorizontalPanel();

	    //add 5 horizontal panels
	    _mainVerticalPanel.add(backButtonpanel);
	    _mainVerticalPanel.add(panel1);
	    _mainVerticalPanel.add(createCapturesPanel(buttonPanel));
	    _mainVerticalPanel.add(createIncidentTablePanel());
	    _mainVerticalPanel.add(submitPanel);
		
	    final Button backButton = new Button("Back");
	    backButtonpanel.add(backButton);
	    
	    backButton.setSize("50px", "30px");
	    backButton.getElement().getStyle().setMarginLeft(0, Unit.PX);
	    backButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {				
				_manager.gotoActivity(SelectPatientActivity.ID);
				
			}
		});

	    
	    //to top panel add 3 vertical panels
	    final VerticalPanel picturePanel = new VerticalPanel();
		final VerticalPanel labelPanel = new VerticalPanel();
		final VerticalPanel inputDetailsPanel = new VerticalPanel();
		panel1.add(labelPanel);
		panel1.add(inputDetailsPanel);
		panel1.add(picturePanel);
		
		//add the labels
		final Label lblName = new Label();
		lblName.setText("Name:  ");
		
		final Label lblDOB = new Label();
		lblDOB.setText("D.O.B.:  ");
		
		final Label lblDoctore = new Label();
		lblDoctore.setText("Doctor:  ");
		labelPanel.add(lblName);
		labelPanel.add(lblDOB);
		labelPanel.add(lblDoctore);
		
		//add the values
		final Label valName = new Label();
		valName.setText(patient.name);
		final Label valDOB = new Label();
		valDOB.setText(patient.dateOfBirth);
		final Label valDoctore = new Label();
		valDoctore.setText(patient.doctor);
		inputDetailsPanel.add(valName);
		inputDetailsPanel.add(valDOB);
		inputDetailsPanel.add(valDoctore);
		
		//add the picture
		Image image = new Image(patient.imageUrl);
		image.setSize("155px", "155px");
		image.getElement().getStyle().setPaddingLeft(10, Unit.PX);
			
		picturePanel.add(image);
				
	    // ...and set it's column span so that it takes up the whole row.
	    //t.getFlexCellFormatter().setColSpan(1, 0, 3);

		final Button uploadButton = new Button("Upload");
		uploadButton.setSize("100px", "30px");
		uploadButton.getElement().getStyle().setMarginLeft(100, Unit.PX);
		uploadButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				Window.alert("Your incident has been uploaded");
			}
		});
		
		submitPanel.add(uploadButton);

		for (Capture capture:_captureLibrary.getCaptures())
		{
			final CaptureId capId = capture.getId();
			Button newButton = new Button(capture.getName());
			newButton.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					_manager.gotoActivity(CaptureActivity.ID, capId);
				}
			});
			buttonPanel.add(newButton);
		}
		
		_incidentTable.clear();
		
		for (Observation observation:_patient.observations)
		{
			_incidentTable.addObservation(observation);
		}
		
		panel.add(_mainVerticalPanel);
		
	}
	
	public void addObservation(Observation newObservation)
	{
		_incidentTable.addObservation(newObservation);
		_patient.observations.add(newObservation);
	}

	@Override
	public void stop() {}
}
